package com.cl.blog.config;

import com.cl.blog.listener.RedisMessageListener2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 付
 * @Date 2024-03-23 17:41
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Configuration
public class RedisConfig {

	@Value("${spring.cache.redis.time-to-live}")
	private Duration timeToLive = Duration.ZERO;

	@Bean("container")
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter, MessageListenerAdapter listenerAdapter2) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) connectionFactory;

		container.setConnectionFactory(lettuceConnectionFactory);

		List<PatternTopic> list = new ArrayList<>();
		list.add(new PatternTopic("test"));
		list.add(new PatternTopic("limit"));
		//设置监听器，可以添加多个监听类addMessageListener(MessageListener listener, Collection<? extends Topic> topics) {
		container.addMessageListener(listenerAdapter2, list);
		return container;
	}

	@Bean
	MessageListenerAdapter listenerAdapter2(RedisMessageListener2 receiver) {
		//设置监听
		return new MessageListenerAdapter(receiver);
	}

	//配置我们自己的redisTemplate  固定模板
	@Bean
	StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
		return new StringRedisTemplate(connectionFactory);
	}

	@Bean
	public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
		RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setConnectionFactory(lettuceConnectionFactory);
		redisTemplate.afterPropertiesSet();//配置立即生效
		return redisTemplate;
	}

	/**
	 * description 配置事务管理器
	 **/
	@Bean
	public PlatformTransactionManager transactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

}
